home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 4 / QRZ Ham Radio Callsign Database - Volume 4.iso / files / satelite / msat09.tgz / VIEWTEXT.C < prev    next >
Text File  |  1994-09-17  |  9KB  |  376 lines

  1. /*
  2.  *   Copyright 1992, 1993, 1994 John Melton (G0ORX/N6LYT)
  3.  *              All Rights Reserved
  4.  *
  5.  *   This program is free software; you can redistribute it and/or modify
  6.  *   it under the terms of the GNU General Public License as published by
  7.  *   the Free Software Foundation; either version 1, or (at your option)
  8.  *   any later version.
  9.  *
  10.  *   This program is distributed in the hope that it will be useful,
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *   GNU General Public License for more details.
  14.  *
  15.  *   You should have received a copy of the GNU General Public License
  16.  *   along with this program; if not, write to the Free Software
  17.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */
  20.  
  21. /*
  22.     viewtext.c
  23.  
  24.     Simple X-Windows program to view an ascii text message directly from
  25.     a pacsat download file.
  26.  
  27.     John Melton
  28.     G0ORX, N6LYT
  29.  
  30.     4 Charlwoods Close
  31.     Copthorne
  32.     West Sussex
  33.     RH10 3QZ
  34.     England
  35.  
  36.     INTERNET:    g0orx@amsat.org
  37.             n6lyt@amsat.org
  38.             john@images.demon.co.uk
  39.             J.D.Melton@slh0613.icl.wins.co.uk
  40.  
  41.     History:
  42.  
  43.     0.1    Initial version.            G0ORX
  44.     0.2    Converted to Xaw.            G4KLX
  45.     0.3    Added display of ZIP and LHA files.    G4KLX
  46. */
  47.  
  48. #include <X11/Intrinsic.h>
  49. #include <X11/StringDefs.h>
  50. #include <X11/Shell.h>
  51. #include <X11/Xaw/Cardinals.h>
  52. #include <X11/Xaw/Form.h>
  53. #include <X11/Xaw/Text.h>
  54. #include <X11/Xaw/AsciiText.h>
  55. #include <X11/Xaw/Label.h>
  56. #include <X11/Xaw/Command.h>
  57. #include <X11/Xaw/Viewport.h>
  58.  
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <unistd.h>
  62. #include <sys/types.h>
  63. #include <sys/wait.h>
  64. #include <fcntl.h>
  65. #include <dirent.h>
  66. #include <signal.h>
  67. #include <time.h>
  68.  
  69. #include "xawutils.h"
  70. #include "header.h"
  71. #include "ftl0.h"
  72.  
  73. Display *dpy;
  74.  
  75. XtAppContext app_context;
  76.  
  77. typedef struct
  78. {
  79.     XFontStruct *bold_font, *button_font, *text_font;
  80. }
  81. Resources;
  82.  
  83. Resources  resources;
  84.  
  85. Widget toplevel, compwindow, quitbutton, replybutton, datawindow;
  86.  
  87. static XtResource resource_list[] =
  88. {
  89.     {"boldFont", XtCFont, XtRFontStruct, sizeof(XFontStruct *),
  90.         XtOffsetOf(Resources, bold_font), XtRString, XtDefaultFont},
  91.     {"buttonFont", XtCFont, XtRFontStruct, sizeof(XFontStruct *),
  92.         XtOffsetOf(Resources, button_font), XtRString, XtDefaultFont},
  93.     {"textFont", XtCFont, XtRFontStruct, sizeof(XFontStruct *),
  94.         XtOffsetOf(Resources, text_font), XtRString, XtDefaultFont}
  95. };
  96.  
  97. static Arg shell_args[] =
  98. {
  99.     {XtNtitle,        (XtArgVal)NULL}
  100. };
  101.  
  102. static Arg form_args[] =
  103. {
  104.     {XtNdefaultDistance,    (XtArgVal)0}
  105. };
  106.  
  107. static Arg button_args[] =
  108. {
  109.     {XtNcallback,        (XtArgVal)NULL},
  110.     {XtNlabel,        (XtArgVal)NULL},
  111.     {XtNfromHoriz,        (XtArgVal)NULL},
  112.     {XtNfont,        (XtArgVal)NULL},
  113.     {XtNresize,        (XtArgVal)False},
  114.     {XtNvertDistance,    (XtArgVal)6},
  115.     {XtNhorizDistance,    (XtArgVal)8},
  116.     {XtNtop,        XtChainTop},
  117.     {XtNbottom,        XtChainTop},
  118.     {XtNleft,        XtChainLeft},
  119.     {XtNright,        XtChainLeft}
  120. };
  121.  
  122. static Arg window_args[] =
  123. {
  124.     {XtNfromVert,        (XtArgVal)NULL},
  125.     {XtNbackground,        (XtArgVal)NULL},
  126.     {XtNfont,        (XtArgVal)NULL},
  127.     {XtNcursor,        (XtArgVal)NULL},
  128.     {XtNwidth,        (XtArgVal)600},
  129.     {XtNheight,        (XtArgVal)250},
  130.     {XtNvertDistance,    (XtArgVal)6},
  131.     {XtNscrollVertical,    XawtextScrollAlways},
  132.     {XtNeditType,        XawtextEdit},
  133.     {XtNtype,        XawAsciiString},
  134.     {XtNdisplayNonprinting,    False},
  135.     {XtNdisplayCaret,    False},
  136.     {XtNwrap,        (XtArgVal)XawtextWrapLine},
  137.     {XtNautoFill,        True}
  138. };
  139.  
  140. char *fileName;
  141.  
  142. void QuitCb(Widget w, XtPointer client_data, XtPointer call_data)
  143. {
  144.     XtDestroyApplicationContext(app_context);
  145.  
  146.     exit(0);
  147. }
  148.  
  149. void ViewText(void)
  150. {
  151.     char *pBuffer;
  152.     FILE *hFile;
  153.     FILE *hOut;
  154.     int headerSize;
  155.     HEADER *pHeader;
  156.     int nBytes;
  157.     int status;
  158.     XawTextPosition pos = 0;
  159.     XawTextBlock tt;
  160.     char command[40], title[20];
  161.     char compFile[20], viewFile[20];
  162.     time_t t;
  163.     
  164.     /* open the file */
  165.     if ((hFile = fopen(fileName, "r")) == NULL)
  166.     {
  167.         MessageBox("Cannot open input file");
  168.         return;
  169.     }
  170.  
  171.     pBuffer = XtMalloc(1024);
  172.  
  173.     /* read in the header */
  174.     nBytes = fread(pBuffer, 1, 1024, hFile);
  175.  
  176.     /* extracting the header */
  177.     if ((pHeader = ExtractHeader(pBuffer, nBytes, &headerSize)) == NULL)
  178.     {
  179.         MessageBox("Cannot extract PACSAT header");
  180.         fclose(hFile);
  181.         return;
  182.     }
  183.  
  184.     strcpy(title, fileName);
  185.  
  186.     if (pHeader->compression != 0 && pHeader->compression != 255)
  187.     {
  188.         time(&t);
  189.     
  190.         sprintf(compFile, "%x.comp", (int)t);
  191.         sprintf(viewFile, "%x.view", (int)t);
  192.     
  193.         fseek(hFile, pHeader->bodyOffset, SEEK_SET);
  194.  
  195.         if ((hOut = fopen(compFile, "w")) == NULL)
  196.         {
  197.             MessageBox("Cannot open output file");
  198.             return;
  199.         }
  200.  
  201.         while ((nBytes = fread(pBuffer, 1, 1024, hFile)) > 0)
  202.             fwrite(pBuffer, 1, nBytes, hOut);
  203.  
  204.         fclose(hFile);
  205.         fclose(hOut);
  206.     
  207.         switch (pHeader->compression)
  208.         {
  209.         case 2:
  210.             sprintf(command, "unzip -p %s > %s", compFile, viewFile);
  211.             strcat(title, " (pkzip)");
  212.             break;
  213.         case 3:
  214.             sprintf(command, "lha -pq %s > %s", compFile, viewFile);
  215.             strcat(title, " (lha)");
  216.             break;
  217.         default:
  218.             break;
  219.         }
  220.  
  221.         status = system(command);
  222.  
  223.         if (status == 127 || status < 0)
  224.         {
  225.             unlink(compFile);
  226.             MessageBox("Cannot run unzip/lha");
  227.             return;
  228.         }
  229.  
  230.         if ((hFile = fopen(viewFile, "r")) == NULL)
  231.         {
  232.             unlink(compFile);
  233.             MessageBox("Cannot open input file");
  234.             return;
  235.         }
  236.     }
  237.     
  238.     XtVaSetValues(toplevel, XtNtitle, title, NULL);
  239.  
  240.     if (strlen(pHeader->title) == 0)
  241.         nBytes = sprintf(pBuffer, "To: %s\nFrom: %s\nTitle: %s\n%s\n\n",
  242.                 pHeader->destination,
  243.                 pHeader->source,
  244.                 pHeader->fileName,
  245.                 "----------------------------------------");
  246.     else
  247.         nBytes = sprintf(pBuffer, "To: %s\nFrom: %s\nTitle: %s\n%s\n\n",
  248.                 pHeader->destination,
  249.                 pHeader->source,
  250.                 pHeader->title,
  251.                 "----------------------------------------");
  252.  
  253.     tt.firstPos = 0;
  254.     tt.ptr      = pBuffer;
  255.     tt.length   = nBytes;
  256.     tt.format   = FMT8BIT;
  257.  
  258.     XawTextReplace(datawindow, pos, pos, &tt);
  259.  
  260.     pos += nBytes;
  261.  
  262.     /* now copy in the text */
  263.     if (pHeader->compression == 0)
  264.         fseek(hFile, pHeader->bodyOffset, SEEK_SET);
  265.  
  266.     while ((nBytes = fread(pBuffer, 1, 1024, hFile)) > 0)
  267.     {
  268.         tt.firstPos = 0;
  269.         tt.ptr      = pBuffer;
  270.         tt.length   = nBytes;
  271.         tt.format   = FMT8BIT;
  272.  
  273.         XawTextReplace(datawindow, pos, pos, &tt);
  274.  
  275.         pos += nBytes;
  276.     }
  277.  
  278.     fclose(hFile);
  279.  
  280.     if (pHeader->compression != 0)
  281.     {
  282.         unlink(compFile);
  283.         unlink(viewFile);
  284.     }
  285.  
  286.     XtFree((char *)pHeader);
  287.     XtFree(pBuffer);
  288. }
  289.  
  290. void signal_child(int s)
  291. {
  292.         int pid, pstatus;
  293.  
  294.     signal(SIGCHLD, signal_child);
  295.  
  296.         pid = wait(&pstatus);
  297. }
  298.  
  299. void ReplyCb(Widget w, XtPointer client_data, XtPointer call_data)
  300. {
  301.         int pid;
  302.  
  303.         if ((pid = fork()) == 0)
  304.         {
  305.                 /* the child process */
  306.                 execlp("message", "message", fileName, NULL);
  307.         }
  308.         else if (pid > 0)
  309.         {
  310.                 /* success */
  311.         }
  312.         else
  313.         {
  314.         MessageBox("Cannot fork message");
  315.         }
  316. }
  317.  
  318. int main(int argc, char **argv)
  319. {
  320.     static XtCallbackRec callback[2];
  321.  
  322.     signal(SIGCHLD, signal_child);
  323.  
  324.     if (argc == 1)
  325.     {
  326.         fprintf(stderr, "usage: viewtext <file-name>\n");
  327.         return(1);
  328.     }
  329.  
  330.     toplevel = XtAppInitialize(&app_context, "Xpb", NULL, 0, &argc, argv,
  331.                 NULL, shell_args, XtNumber(shell_args));
  332.  
  333.     dpy  = XtDisplay(toplevel);
  334.  
  335.     XtGetApplicationResources(toplevel, &resources,
  336.                 resource_list, XtNumber(resource_list),
  337.                 NULL, ZERO);
  338.  
  339.     compwindow = XtCreateManagedWidget("appForm", formWidgetClass,
  340.                 toplevel, form_args, XtNumber(form_args));
  341.  
  342.     callback[0].callback = QuitCb;
  343.     callback[0].closure  = toplevel;
  344.     button_args[0].value = (XtArgVal)callback;
  345.     button_args[1].value = (XtArgVal)"Quit";
  346.     button_args[3].value = (XtArgVal)resources.button_font;
  347.     quitbutton = XtCreateManagedWidget("quitButton", commandWidgetClass,
  348.                 compwindow, button_args, XtNumber(button_args));
  349.  
  350.     callback[0].callback = ReplyCb;
  351.     callback[0].closure  = toplevel;
  352.     button_args[0].value = (XtArgVal)callback;
  353.     button_args[1].value = (XtArgVal)"Reply";
  354.     button_args[2].value = (XtArgVal)quitbutton;
  355.     replybutton = XtCreateManagedWidget("replyButton", commandWidgetClass,
  356.                 compwindow, button_args, XtNumber(button_args));
  357.  
  358.     window_args[0].value = (XtArgVal)quitbutton;
  359.     window_args[1].value = (XtArgVal)WhitePixel(dpy, DefaultScreen(dpy));
  360.     window_args[2].value = (XtArgVal)resources.text_font;
  361.     datawindow = XtCreateManagedWidget("viewText", asciiTextWidgetClass,
  362.                 compwindow, window_args, XtNumber(window_args));
  363.  
  364.     createMessagePopup(resources.bold_font, resources.button_font);
  365.  
  366.     fileName = argv[1];
  367.     ViewText();
  368.  
  369.     XtRealizeWidget(toplevel);
  370.                 
  371.     XtAppMainLoop(app_context);
  372.     
  373.     return(0);
  374. }
  375.  
  376.